package com.xz.queue;

import java.util.concurrent.BlockingQueue;

/**
 * Created by YLZX-Z0107 on 2014/7/20.
 */
public class Consumer implements Runnable {
    public Consumer(BlockingQueue<String> urls) {
        this.urls = urls;
    }

    @Override
    public void run() {
        System.out.println("开始消费. queue size: " + urls.size());
        boolean isRunning = true;
        while (isRunning) {
            sleep();
            String result = "";
            try {
                result = urls.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("消费了 " + result + " . queue size: " + urls.size());
        }
        System.exit(0);
    }

    private BlockingQueue<String> urls;
    private int DEFAULT_SLEEP = 1500;

    private void sleep() {
        try {
            Thread.sleep(DEFAULT_SLEEP);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
